package com.jinxiangyang.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jinxiangyang.domain.OrderInfo;
import com.jinxiangyang.domain.Subject;
import com.jinxiangyang.domain.UserInfo;
import com.jinxiangyang.service.OrderInfoService;
import com.jinxiangyang.service.SubjectService;
import com.jinxiangyang.service.UserInfoService;
import com.jinxiangyang.utils.SecurityUtils;
import com.jinxiangyang.utils.result.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @File PersonalController.java
 * @Author lzq
 * @Create 2023/8/23 14:19
 */
@Api(tags = "个人中心接口")
@RequestMapping("/personal")
@RestController
public class PersonalController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private OrderInfoService orderInfoService;

    @PostMapping("/{id}/{password}")
    @ApiOperation("修改用户密码")
    public AjaxResult updateUserInfo(@PathVariable Integer id,@PathVariable String password) {
        String encryptPassword = SecurityUtils.encryptPassword(password);
        UpdateWrapper<UserInfo> uw = new UpdateWrapper<>();
        uw.eq("id",id);
        uw.set("password",encryptPassword);
        if (userInfoService.update(null,uw)){
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }

    @GetMapping("/userInfo/{id}")
    @ApiOperation("获取用户信息")
    public AjaxResult getUserInfo(@PathVariable Integer id) {
        UserInfo userInfo = userInfoService.getById(id);
        return AjaxResult.success(userInfo);
    }

    @GetMapping("/order/{id}")
    @ApiOperation("获取用户订单信息")
    public AjaxResult getOrder(@PathVariable Integer id) {
        QueryWrapper<OrderInfo> qw = new QueryWrapper<>();
        qw.eq("user_id", id);
        List<OrderInfo> orderInfoList = orderInfoService.list(qw);
        return AjaxResult.success(orderInfoList);
    }
}
